package com.woniuxy.user.controller;

import com.woniuxy.user.dto.LoginRequest;
import com.woniuxy.user.dto.LoginResponse;
import com.woniuxy.user.service.AuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 认证控制器
 */
@Slf4j
@RestController
@RequestMapping("/auth")
@CrossOrigin(origins = "*")
public class AuthController {
    
    @Autowired
    private AuthService authService;
    
    /**
     * 用户登录
     */
    @PostMapping("/login")
    public ResponseEntity<LoginResponse> login(@Valid @RequestBody LoginRequest loginRequest) {
        log.info("用户登录请求，用户名：{}", loginRequest.getUsername());
        
        try {
            LoginResponse response = authService.login(loginRequest);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("登录失败", e);
            LoginResponse errorResponse = new LoginResponse();
            errorResponse.setSuccess(false);
            errorResponse.setMessage(e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
    
    /**
     * 验证Token
     */
    @GetMapping("/validate")
    public ResponseEntity<Map<String, Object>> validateToken(@RequestHeader("Authorization") String token) {
        log.info("验证Token请求");
        
        try {
            // 移除 "Bearer " 前缀
            if (token.startsWith("Bearer ")) {
                token = token.substring(7);
            }
            
            boolean isValid = authService.validateToken(token);
            Map<String, Object> result = new HashMap<>();
            result.put("valid", isValid);
            result.put("message", isValid ? "Token有效" : "Token无效");
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("Token验证失败", e);
            Map<String, Object> result = new HashMap<>();
            result.put("valid", false);
            result.put("message", "Token验证失败：" + e.getMessage());
            return ResponseEntity.badRequest().body(result);
        }
    }
    
    /**
     * 用户登出
     */
    @PostMapping("/logout")
    public ResponseEntity<Map<String, String>> logout(@RequestHeader("Authorization") String token) {
        log.info("用户登出请求");
        
        try {
            // 移除 "Bearer " 前缀
            if (token.startsWith("Bearer ")) {
                token = token.substring(7);
            }
            
            authService.logout(token);
            Map<String, String> result = new HashMap<>();
            result.put("message", "登出成功");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("登出失败", e);
            Map<String, String> result = new HashMap<>();
            result.put("message", "登出失败：" + e.getMessage());
            return ResponseEntity.badRequest().body(result);
        }
    }
}

